package com.itchu.system.controller;

import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.Authority;
import com.itchu.system.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/authority")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    /**
     * 查找权限的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<Authority>> findByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc
    ) {
        PageResult<Authority> list = authorityService.findByPage(key,page,rows,sortBy,desc);
        return ResponseEntity.ok(list);
    }

    /**
     * 查找出所有权限
     * 你可能会有这样的疑问：上面不是查找出权限的分页了吗，为什么还要查一遍
     * 因为这个是树形组件要用到，所以要查出所有
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity<List<Authority>> AllAuthority() {
        List<Authority> list = authorityService.AllAuthority();
        return ResponseEntity.ok(list);
    }

    /**
     * 保存权限
     * @param authority
     * @return
     */
    @PostMapping("/save")
    public ResponseEntity<Void> addAuthority(Authority authority) {

        authorityService.addAuthority(authority);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改权限
     * @param authority
     * @return
     */
    @PutMapping("/save")
    public ResponseEntity<Void> updateAuthority(Authority authority) {
        authorityService.updateAuthority(authority);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseEntity<Void> deletAuthority(
            @RequestParam("id")Long id) {
        authorityService.deletAuthority(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据权限的父id查找出父级权限
     * @param pid
     * @return
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<Authority>> showAuthorityByPid(
            @RequestParam("pid") Long pid
    ) {
        List<Authority> list = authorityService.showAuthorityByPid(pid);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据权限id查找权限
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public ResponseEntity<Authority> findById(
            @RequestParam(value = "id") Long id
    ) {
        Authority authority = authorityService.findById(id);
        return ResponseEntity.ok(authority);
    }
}
